﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UFIDA.U8.UAP.UI.Runtime.Common;
using UFIDA.U8.UAP.UI.Runtime.Model;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ufida.u8.uap.plugin.NRJFA
{

    //财务
    public class FAclass : ReceiptPluginBase
    {
        public FAclass()
        {
        }
        int sCardNumMaxID = 0;//固定资产对象表卡片编号最大ID
        public static string tempOfCWUAP = string.Empty;//获取业务卡片编号给uapTou8
        string tempId = string.Empty;//临时ID
        //值更新之后的接口
        [IsImplementAttribute(true)]
        override
        public void CellChanged(CellChangeEventArgs para, BusinessProxy businessObject, VoucherProxy voucherObject)
        {
            BusinessRow bu = businessObject.Rows[para.RowKey];

            switch (para.ColumnName)
            {
                case "cNo":
                    tempId = bu.Cells["cNo"].Value.Trim();//获取卡片上的卡片编号给临时ID赋值
                    //select_fa_objects_sCardNumMaxID(voucherObject);//获取fa_objects最大卡片ID
                    //MessageBox.Show("最大卡片ID" + sCardNumMaxID);
                    //MessageBox.Show("更新卡片编号已,请重新输入！");
                    //string temp = "100000";
                    //temp = Convert.ToString(Convert.ToInt32(temp) + sCardNumMaxID);//临时卡片编号=临时卡片编号+最大卡片编号
                    //MessageBox.Show("temp::" + temp);
                    //temp = temp.Substring(temp.Length - 5);//截取总长度的后5位
                    //MessageBox.Show("temp22::" + temp);
                    //voucherObject.Businesses["NRJ001_0001_E001_1_1"].Cells["cNo"].Value = temp.ToString();
                    //tempOfCWUAP = temp.ToString();
                    //MessageBox.Show("temp33" + temp);
                    //cMaker = bu.Cells["cMaker"].Value.Trim();
                    if (select_fa_Cards_sCardNum(voucherObject))//查询U8卡片编号是否存在
                    {
                        MessageBox.Show("卡片编号已存在,请重新输入！");//获取信息发送人
                        voucherObject.Businesses["NRJ001_0001_E001_1_1"].Cells["cNo"].Value = "";
                    }
                    break;
                case "sAssetNum":
                    tempId = bu.Cells["sAssetNum"].Value.Trim();//获取卡片上的固定资产编号给临时ID赋值
                    if (select_fa_DeprTransactions_sDeprAssetNum(voucherObject))//查询U8固定资产编号是否存在
                    {
                        MessageBox.Show("固定资产编号已存在,请重新输入！");
                        voucherObject.Businesses["NRJ001_0001_E001_1_1"].Cells["sAssetNum"].Value = "";
                    }
                    break;
                #region
                //case "dblBV":
                //     if (Convert.ToDouble(bu.Cells["dblBV"].Value.Trim()) > Convert.ToDouble(bu.Cells["dblValue"].Value.Trim()))//净残值>原值
                //{
                //    bu.Cells["dblBV"].Value = "0";//净残值为0
                //    bu.Cells["dblBVRate"].Value = "0";//净残值率为0
                //    MessageBox.Show("净产值不能大于原值！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //}
                //else
                //{
                //    bu.Cells["dblBVRate"].Value = Convert.ToString(roundDouble(Convert.ToDouble(bu.Cells["dblBVdblValue"].Value.Trim()) / Convert.ToDouble(bu.Cells["dblValue"].Value.Trim()), 2));
                //    //净残值率=净残值/原值
                //}
                //    break;

                //case "ljzj":
                //    if (Convert.ToDouble(bu.Cells["ljzj"].Value.Trim()) > Convert.ToDouble(bu.Cells["dblValue"].Value.Trim()))//如果累计折旧大于原值
                //    {
                //        bu.Cells["ljzj"].Value = "0";//累计折旧为0
                //        bu.Cells["netValue"].Value = Convert.ToString(Convert.ToDouble(bu.Cells["dblValue"].Value.Trim()));//净值=原值
                //        MessageBox.Show("累计折旧不能大于原值！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //    }
                //    else
                //    {
                //        bu.Cells["netValue"].Value = Convert.ToString(Convert.ToDouble(bu.Cells["dblValue"].Value.Trim()) - Convert.ToDouble(bu.Cells["ljzj"].Value.Trim()));
                //        //净值=原值-累计折旧
                //    }
                //    break;

                //case "netValue":
                //    if (Convert.ToDouble(bu.Cells["netValue"].Value.Trim()) > Convert.ToDouble(bu.Cells["dblValue"].Value.Trim()))//如果净值>原值
                //{
                //    bu.Cells["netValue"].Value = Convert.ToString(Convert.ToDouble(bu.Cells["dblValue"].Value.Trim()) - Convert.ToDouble(bu.Cells["ljzj"].Value.Trim()));
                //    //净值=原值-累计折旧
                //    MessageBox.Show("净值不能大于原值！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //}
                //else
                //{
                //    bu.Cells["ljzj"].Value = Convert.ToString(Convert.ToDouble(bu.Cells["dblValue"].Value.Trim()) - Convert.ToDouble(bu.Cells["netValue"].Value.Trim()));
                //    //累计折旧=原值-净值
                //}
                //break;
                #endregion
            }

            if (para.ColumnName == "dblBV") //净残值
            {
                if (Convert.ToDouble(bu.Cells["dblBV"].Value.Trim()) > Convert.ToDouble(bu.Cells["dblValue"].Value.Trim()))//净残值>原值
                {
                    bu.Cells["dblBV"].Value = "0";//净残值为0
                    bu.Cells["dblBVRate"].Value = "0";//净残值率为0
                    MessageBox.Show("净产值不能大于原值！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    bu.Cells["dblBVRate"].Value = Convert.ToString(roundDouble(Convert.ToDouble(bu.Cells["dblBVdblValue"].Value.Trim()) / Convert.ToDouble(bu.Cells["dblValue"].Value.Trim()), 2));
                    //净残值率=净残值/原值
                }
            }

            if (para.ColumnName == "ljzj")//累计折旧
            {
                if (Convert.ToDouble(bu.Cells["ljzj"].Value.Trim()) > Convert.ToDouble(bu.Cells["dblValue"].Value.Trim()))//如果累计折旧大于原值
                {
                    bu.Cells["ljzj"].Value = "0";//累计折旧为0
                    bu.Cells["netValue"].Value = Convert.ToString(Convert.ToDouble(bu.Cells["dblValue"].Value.Trim()));//净值=原值
                    MessageBox.Show("累计折旧不能大于原值！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    bu.Cells["netValue"].Value = Convert.ToString(Convert.ToDouble(bu.Cells["dblValue"].Value.Trim()) - Convert.ToDouble(bu.Cells["ljzj"].Value.Trim()));
                    //净值=原值-累计折旧
                }
            }

            if (para.ColumnName == "netValue")//净值
            {
                if (Convert.ToDouble(bu.Cells["netValue"].Value.Trim()) > Convert.ToDouble(bu.Cells["dblValue"].Value.Trim()))//如果净值>原值
                {
                    bu.Cells["netValue"].Value = Convert.ToString(Convert.ToDouble(bu.Cells["dblValue"].Value.Trim()) - Convert.ToDouble(bu.Cells["ljzj"].Value.Trim()));
                    //净值=原值-累计折旧
                    MessageBox.Show("净值不能大于原值！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    bu.Cells["ljzj"].Value = Convert.ToString(Convert.ToDouble(bu.Cells["dblValue"].Value.Trim()) - Convert.ToDouble(bu.Cells["netValue"].Value.Trim()));
                    //累计折旧=原值-净值
                }
            }
        }

        /// <summary>
        /// double小数位的处理
        /// </summary>
        /// <param name="val"></param>
        /// <param name="precision"></param>
        /// <returns></returns>
        public Double roundDouble(double val, int precision)
        {
            Double ret = 0.0;
            try
            {
                double factor = Math.Pow(10, precision);
                ret = Math.Floor(val * factor + 0.5) / factor;
            }
            catch (Exception e)
            {
                e.ToString();
            }
            return ret;
        }

        /// <summary>
        /// 查询U8卡片编号是否存在
        /// </summary>
        /// <param name="voucherObject"></param>
        public bool select_fa_Cards_sCardNum(VoucherProxy voucherObject)
        {
            int isok = 0;//U8卡片编号
            using (SqlConnection con = new SqlConnection(voucherObject.LoginInfo.UFDataSqlConStr))
            {
                con.Open();
                SqlTransaction tran = con.BeginTransaction(System.Data.IsolationLevel.RepeatableRead);
                con.Close();
                try
                {
                    string sql = string.Format("select count(sCardNum) from fa_Cards where sCardNum='{0}'", tempId);
                    SqlCommand com = new SqlCommand(sql, con);
                    con.Open();
                    isok = Convert.ToInt32(com.ExecuteScalar());
                }
                catch (SqlException e)
                {
                    MessageBox.Show("错误信息2：" + e.ToString());
                }
                finally
                {
                    if (con != null && con.State != System.Data.ConnectionState.Closed)
                    {
                        con.Close();
                    }
                }
                if (isok > 0)
                {
                    return true;
                }
                return false;
            }
        }

        /// <summary>
        /// 查询U8固定资产编号是否存在
        /// </summary>
        /// <param name="voucherObject"></param>
        /// <returns></returns>
        public bool select_fa_DeprTransactions_sDeprAssetNum(VoucherProxy voucherObject)
        {
            int isok = 0;
            using (SqlConnection con = new SqlConnection(voucherObject.LoginInfo.UFDataSqlConStr))
            {
                con.Open();
                SqlTransaction tran = con.BeginTransaction(System.Data.IsolationLevel.RepeatableRead);
                con.Close();
                try
                {
                    string sql = string.Format("select count(sDeprAssetNum) from fa_DeprTransactions where sDeprAssetNum='{0}'", tempId);
                    SqlCommand com = new SqlCommand(sql, con);
                    con.Open();
                    isok = Convert.ToInt32(com.ExecuteScalar());
                }
                catch (SqlException e)
                {
                    MessageBox.Show("错误信息1：" + e.ToString());
                }
                finally
                {
                    if (con != null && con.State != System.Data.ConnectionState.Closed)
                    {
                        con.Close();
                    }
                }
                if (isok > 0)
                {
                    return true;
                }
                return false;
            }
        }

        public static string id = string.Empty;//UAP卡片编号
        override
        public IButtonEventHandler GetButtonEventHandler(UFIDA.U8.UAP.UI.Runtime.Common.VoucherButtonArgs ButtonArgs, VoucherProxy voucherObject)
        {
            //if (ButtonArgs.ButtonKey == "btnModifyVoucher")
            //{
            //    MessageBox.Show("22222222");
            //    voucherObject.Businesses["NRJ001_0001_E001_1_1"].Cells["cNo"].ReadOnly = false;
            //    voucherObject.Businesses["cNo"].ReadOnly = false;
            //}
            if (ButtonArgs.ButtonKey == "btnSaveVoucher")
            {
                id = voucherObject.Businesses["NRJ001_0001_E001_1_1"].Cells["cNo"].Value;
                MessageBox.Show("z转U8");
                return new uapTOu8();//UAP插入U8
            }
            return null;
        }

        /// <summary>
        /// 获取fa_objects最大卡片ID
        /// </summary>
        private void select_fa_objects_sCardNumMaxID(VoucherProxy voucherObject)
        {
            using (SqlConnection con = new SqlConnection(voucherObject.LoginInfo.UFDataSqlConStr))
            {
                con.Open();
                SqlTransaction tran = con.BeginTransaction(System.Data.IsolationLevel.RepeatableRead);
                con.Close();
                try
                {
                    string sql = string.Format("select  lMaxID from fa_Objects  where iObjectNum=5");
                    SqlCommand com = new SqlCommand(sql, con);
                    con.Open();
                    sCardNumMaxID = Convert.ToInt32(com.ExecuteScalar());
                    sCardNumMaxID++;
                    //sCardID=Convert.ToString(sCardID ++);
                }
                catch
                { }
                finally
                {
                    if (con != null && con.State != System.Data.ConnectionState.Closed)
                    {
                        con.Close();
                    }
                }
            }
        }
    }
}


